﻿using Abide_Halo_Library.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Abide_Halo_Library.Halo_2_Beta_Map
{
    public static class WindowsForms
    {
        public static void BuildTreeNodes(this Halo2BetaMap map, ref TreeView treeView, TagSorting sorting)
        {
            TreeNodeCollection collection = treeView.Nodes;
            BuildTreeNodes(map, ref collection, sorting);
        }

        public static void BuildTreeNodes(this Halo2BetaMap map, ref TreeNode treeNode, TagSorting sorting)
        {
            TreeNodeCollection collection = treeNode.Nodes;
            BuildTreeNodes(map, ref collection, sorting);
        }

        private static void BuildTreeNodes(this Halo2BetaMap map, ref TreeNodeCollection collection, TagSorting sorting)
        {
            //Build Nodes
            switch (sorting)
            {
                case TagSorting.Class:
                    BuildClassList(map.IndexEntries, ref collection);
                    break;
                case TagSorting.Hierarchical:
                    BuildHierarchialList(map.IndexEntries, ref collection);
                    break;
            }
        }

        private static void BuildClassList(IndexEntry[] entries, ref TreeNodeCollection collection)
        {
            //Prepare
            TreeNode TagNode = null;
            TreeNode ClassNode = null;

            //Loop
            foreach (IndexEntry entry in entries)
            {
                //Get (or create) Class Node
                if (collection.ContainsKey(entry.Class))
                    ClassNode = collection[entry.Class]; //Get
                else
                {
                    //Create
                    ClassNode = new TreeNode(string.Format("[{0}] - {1}", entry.Class, GetTypeDescription(entry.Class)));
                    ClassNode.SelectedImageIndex = 1;
                    ClassNode.ImageIndex = 1;
                    ClassNode.Name = entry.Class;
                    ClassNode.Tag = -1;

                    //Add
                    collection.Add(ClassNode);
                }

                //Create TagNode
                TagNode = new TreeNode(entry.FileName);
                HandleClassIcon(ref TagNode, entry.Class);
                TagNode.Name = entry.Ident.ToString("X");
                TagNode.Tag = entry.Ident;

                //Add
                ClassNode.Nodes.Add(TagNode);
            }
        }

        private static void BuildHierarchialList(IndexEntry[] entries, ref TreeNodeCollection collection)
        {
            //Prepare
            TreeNode Node = null;
            TreeNodeCollection CurrentCollection = null;
            string[] Parts = null;

            //Loop
            foreach (IndexEntry entry in entries)
            {
                //Prepare
                Parts = entry.FileName.Split('\\');
                CurrentCollection = collection;

                //Loop
                for (int i = 0; i < Parts.Length - 1; i++)
                {
                    if (CurrentCollection.ContainsKey(Parts[i]))
                        CurrentCollection = CurrentCollection[Parts[i]].Nodes;
                    else
                    {
                        Node = new TreeNode(Parts[i]);
                        Node.SelectedImageIndex = 1;
                        Node.ImageIndex = 1;
                        Node.Name = Parts[i];
                        Node.Tag = -1;

                        CurrentCollection.Add(Node);
                        CurrentCollection = Node.Nodes;
                    }
                }

                //Create Node
                Node = new TreeNode(string.Format("{0}.{1}", Parts[Parts.Length - 1], entry.Class));
                HandleClassIcon(ref Node, entry.Class);
                Node.Name = entry.Ident.ToString("X");
                Node.Tag = entry.Ident;

                //Add
                CurrentCollection.Add(Node);
            }
        }

        private static string GetTypeDescription(string Type)
        {
            string Description = string.Empty;
            switch (Type.Trim())
            {
                case "matg":
                    Description = "Match Globals";
                    break;

                case "sncl":
                    Description = "Sound Classes";
                    break;

                case "spk!":
                    Description = "Sound Dialoge";
                    break;

                case "sky":
                    Description = "Sky";
                    break;

                case "scnr":
                    Description = "Scenario";
                    break;

                case "mode":
                    Description = "Model";
                    break;

                case "shad":
                    Description = "Shading Material";
                    break;

                case "stem":
                    Description = "Shader Templates";
                    break;

                case "spas":
                    Description = "Shader Passes";
                    break;

                case "bitm":
                    Description = "Bitmap";
                    break;

                case "vrtx":
                    Description = "Vertex Shaders";
                    break;

                case "lens":
                    Description = "Lense Flares";
                    break;

                case "scen":
                    Description = "Scenery Objects";
                    break;

                case "hlmt":
                    Description = "Object Properties";
                    break;

                case "jmad":
                    Description = "JMS Animation Graph";
                    break;

                case "effe":
                    Description = "Effect";
                    break;

                case "prt3":
                    Description = "Particle";
                    break;

                case "pmov":
                    Description = "Particle Physics";
                    break;

                case "lsnd":
                    Description = "Looping Sound";
                    break;

                case "snd!":
                    Description = "Sound";
                    break;

                case "coll":
                    Description = "Collision Geometry";
                    break;

                case "phmo":
                    Description = "Physics Model";
                    break;

                case "PRTM":
                    Description = "Particle Model";
                    break;

                case "ssce":
                    Description = "Sound Scenery";
                    break;

                case "ligh":
                    Description = "Light Object";
                    break;

                case "itmc":
                    Description = "Item Collections";
                    break;

                case "weap":
                    Description = "Weapon";
                    break;

                case "foot":
                    Description = "Material Effect";
                    break;

                case "deca":
                    Description = "Decal";
                    break;

                case "jpt!":
                    Description = "Damage Effect";
                    break;

                case "nhdt":
                    Description = "New HUD Interface";
                    break;

                case "eqip":
                    Description = "Equipment";
                    break;

                case "proj":
                    Description = "Projectile";
                    break;

                case "cont":
                    Description = "Contrail";
                    break;

                case "pphy":
                    Description = "Point Physics";
                    break;

                case "MGS2":
                    Description = "Light Volume";
                    break;

                case "vehc":
                    Description = "Vehicle Collection";
                    break;

                case "vehi":
                    Description = "Vehicle";
                    break;

                case "bloc":
                    Description = "Obstacle";
                    break;

                case "trak":
                    Description = "Camera";
                    break;

                case "ant!":
                    Description = "Antenna";
                    break;

                case "snde":
                    Description = "Environmental Sound";
                    break;

                case "DECR":
                    Description = "Decorator";
                    break;

                case "unic":
                    Description = "Unicode String List";
                    break;

                case "mulg":
                    Description = "Multiplayer Globals";
                    break;

                case "sfx+":
                    Description = "Sound Effects";
                    break;

                case "<fx>":
                    Description = "DSP Effects";
                    break;

                case "snmx":
                    Description = "Sound Mix";
                    break;

                case "adlg":
                    Description = "AI Dialoge";
                    break;

                case "colo":
                    Description = "Color";
                    break;

                case "hudg":
                    Description = "Display Globals";
                    break;

                case "wgtz":
                    Description = "Widget Collections";
                    break;

                case "wigl":
                    Description = "Widget Globals";
                    break;

                case "skin":
                    Description = "UI Skin";
                    break;

                case "wgit":
                    Description = "Widget";
                    break;

                case "bipd":
                    Description = "Biped";
                    break;

                case "garb":
                    Description = "Garbage";
                    break;

                case "udlg":
                    Description = "Unit Dialog";
                    break;

                case "bsdt":
                    Description = "Breakable Surface";
                    break;

                case "gldf":
                    Description = "Global Lighting";
                    break;

                case "clwd":
                    Description = "Blowing Cloth";
                    break;

                case "tdtl":
                    Description = "Beam Trail";
                    break;

                case "char":
                    Description = "Character";
                    break;

                case "styl":
                    Description = "Situational AI Style";
                    break;

                case "crea":
                    Description = "Creature";
                    break;

                case "weat":
                    Description = "Weather";
                    break;

                case "mach":
                    Description = "Machine";
                    break;

                case "mdlg":
                    Description = "Level Specific Dialog";
                    break;

                case "egor":
                    Description = "Screen FX";
                    break;

                case "ctrl":
                    Description = "Control";
                    break;

                case "phys":
                    Description = "Physics";
                    break;

                case "fog":
                    Description = "Cluster Fog";
                    break;

                case "fpch":
                    Description = "Fog Patch";
                    break;

                case "goof":
                    Description = "Goofy";
                    break;

                case "sily":
                    Description = "Silly Goose";
                    break;

                case "ltmp":
                    Description = "Lightmap";
                    break;

                case "sbsp":
                    Description = "Structure Binary Separation Plane";
                    break;

                case "ugh!":
                    Description = "Sound Diagnostic";
                    break;

                default:
                    Description = "<No Description Set>";
                    break;
            }

            return Description;
        }

        private static void HandleClassIcon(ref System.Windows.Forms.TreeNode Node, string Class)
        {
            //Check Class...
            switch (Class)
            {
                case "bitm":
                    Node.ImageIndex = 3;
                    Node.SelectedImageIndex = 3;
                    break;
                case "snd!":
                    Node.ImageIndex = 4;
                    Node.SelectedImageIndex = 4;
                    break;

                case "nhdt":
                    Node.ImageIndex = 6;
                    Node.SelectedImageIndex = 6;
                    break;

                case "sbsp":
                    Node.ImageIndex = 5;
                    Node.SelectedImageIndex = 5;
                    break;

                default:
                    Node.ImageIndex = 2;
                    Node.SelectedImageIndex = 2;
                    break;
            }
        }
    }
}
